#include "stdafx.h"
#include <XMain.h>
#include <OpenGL\freeglut.h>
#include <sstream>

XString::XString()
{
	String = "";
}

XString::XString(const char* str)
{
	String = str;
}

XString::XString(std::string str)
{
	String = str;
}

XString::XString(int number)
{
	std::ostringstream ss;
	ss << number;
	String = ss.str();
}

XHash::XHash()
{
	String = "";
	Hash = 0;
}

XHash::XHash(XString str)
{
	String = str;
	Hash = _getHash(str.String.c_str());
}

int XHash::_getHash(const char* str)
{
	int hash = 0;
	while (*str)
	{
		hash = hash * 101  +  *str++;
	}

	return hash;
}

bool XHash::operator== (XHash hash)
{
	if(hash.Hash == Hash)
		return true;
	else
		return false;
}

bool XHash::operator==(const char* str)
{
	if(_getHash(str) == Hash)
		return true;
	else
		return false;
}



XColor::XColor() 
{
	R = 255;
	G = 255;
	B = 255;
	A = 255;
}

XColor::XColor(float r, float g, float b, float a) 
{
	R=r;
	G=g;
	B=b; 
	A=a;
}

XColor XColor::operator*(float val)
{
	return XColor(R*val,G*val,B*val,A*val);
}

XColor XColor::operator-(XColor color)
{
	return XColor(R-color.R,G-color.G,B-color.B,A-color.A);
}

XColor XColor::operator+(XColor color)
{
	return XColor(R+color.R,G+color.G,B+color.B,A+color.A);
}

XColor XColor::operator/(float val)
{
	return XColor(R/val,G/val,B/val,A/val);
}

XColor XColor::operator+=(XColor color)
{
	R += color.R;
	G += color.G;
	B += color.B;
	A += color.A;

	return *this;
}

XPosition::XPosition()
{
	X = 0;
	Y = 0;
}

XPosition::XPosition(float x, float y)
{
	X=x;
	Y=y;
}

void XPosition::operator=(XVertex vert)
{
	X = vert.X;
	Y = vert.Y;
}

XVector::XVector()
{
	X = 0;
	Y = 0;
}

XVector::XVector(float x, float y) 
{
	X=x;
	Y=y;
}

XVector XVector::operator*(float val)
{
	return XVector(X*val, Y*val);
}

XVector XVector::operator/(float val)
{
	return XVector(X/val, Y/val);
}

XVector XVector::operator-(XVector vec)
{
	return XVector(X-vec.X, Y-vec.Y);
}

XVector XVector::operator+(XVector vec)
{
	return XVector(X+vec.X, Y+vec.Y);
}

XVector XVector::operator+=(XVector vec)
{
	X += vec.X;
	Y += vec.Y;

	return *this;
}

XVertex::XVertex()
{
	X = 0;
	Y = 0;
}

XVertex::XVertex(int x, int y)
{
	X=x;
	Y=y;
}

void XVertex::operator=(XPosition pos)
{
	X = pos.X;
	Y = pos.Y;
}


XRectangle::XRectangle()
{
	X = 0;
	Y = 0;
	W = 0;
	H = 0;
}

XRectangle::XRectangle(float x, float y, float w, float h) 
{
	X=x;
	Y=y;
	W=w;
	H=h;
}

bool XRectangle::Colide(XRectangle rect)
{
	bool xCollision = true;
	bool yCollision = true;

	if(rect.X > X + W)
		xCollision = false;

	if(rect.X + rect.W < X)
		xCollision = false;

	if(rect.Y > Y + H)
		yCollision = false;

	if(rect.Y + rect.H < Y)
		yCollision = false;

	if(xCollision && yCollision)
		return true;
	else
		return false;
}

bool XRectangle::Colide(XPosition pos)
{
	bool xCollision = false;
	bool yCollision = false;

	if(pos.X > X && pos.X < X + W)
		xCollision = true;

	if(pos.Y > Y && pos.Y < Y + H)
		yCollision = true;

	if(xCollision && yCollision)
		return true;
	else
		return false;
}

bool XRectangle::operator==(XRectangle rect)
{
	if(X == rect.X && Y == rect.Y && W == rect.W && H == rect.H)
		return true;
	else
		return false;
}

void XSetAddictiveBlend()
{
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}

void XEXPORT_DLL XSetNormalBlend()
{
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void XPushTransformation()
{
	glPushMatrix();
}

void XPopTransformation()
{
	glPopMatrix();
}

void XSetColor(XColor color)
{
	glColor4f((float)color.R/255, (float)color.G/255, (float)color.B/255, (float)color.A/255);
}

void XSetTranslation(XVector vec)
{
	glTranslatef(vec.X, vec.Y, 0);
}

void XSetRotation(int angle)
{
	glRotatef(angle, 0, 0, 1);
}

void XSetScale(float scale)
{
	glScalef(scale, scale, 0);
}